1 % Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite
3 {\ttfamily \raggedright {
5 \mbox{}\textit{\textcolor{Brown
}{/*
}} \\
6 \mbox{}\textit{\textcolor{Brown
}{\ \ Graham\ Scan.
}} \\
7 \mbox{}\textit{\textcolor{Brown
}{\ */
}} \\
8 \mbox{}\textbf{\textcolor{RoyalBlue
}{\#include
}}\
\texttt{\textcolor{Red
}{$<$iostream$>$
}} \\
9 \mbox{}\textbf{\textcolor{RoyalBlue
}{\#include
}}\
\texttt{\textcolor{Red
}{$<$vector$>$
}} \\
10 \mbox{}\textbf{\textcolor{RoyalBlue
}{\#include
}}\
\texttt{\textcolor{Red
}{$<$algorithm$>$
}} \\
11 \mbox{}\textbf{\textcolor{RoyalBlue
}{\#include
}}\
\texttt{\textcolor{Red
}{$<$iterator$>$
}} \\
12 \mbox{}\textbf{\textcolor{RoyalBlue
}{\#include
}}\
\texttt{\textcolor{Red
}{$<$math.h$>$
}} \\
13 \mbox{}\textbf{\textcolor{RoyalBlue
}{\#include
}}\
\texttt{\textcolor{Red
}{$<$stdio.h$>$
}} \\
15 \mbox{}\textbf{\textcolor{Blue
}{using
}}\
\textbf{\textcolor{Blue
}{namespace
}}\ std
\textcolor{BrickRed
}{;
} \\
17 \mbox{}\textbf{\textcolor{Blue
}{const
}}\
\textcolor{ForestGreen
}{double
}\ pi\
\textcolor{BrickRed
}{=
}\
\textcolor{Purple
}{2}\textcolor{BrickRed
}{*
}\textbf{\textcolor{Black
}{acos
}}\textcolor{BrickRed
}{(
}\textcolor{Purple
}{0}\textcolor{BrickRed
}{);
} \\
19 \mbox{}\textbf{\textcolor{Blue
}{struct
}}\ point
\textcolor{Red
}{\
{} \\
20 \mbox{}\ \
\textcolor{ForestGreen
}{int
}\ x
\textcolor{BrickRed
}{,
}y
\textcolor{BrickRed
}{;
} \\
21 \mbox{}\ \
\textbf{\textcolor{Black
}{point
}}\textcolor{BrickRed
}{()
}\
\textcolor{Red
}{\
{\
}} \\
22 \mbox{}\ \
\textbf{\textcolor{Black
}{point
}}\textcolor{BrickRed
}{(
}\textcolor{ForestGreen
}{int
}\ X
\textcolor{BrickRed
}{,
}\
\textcolor{ForestGreen
}{int
}\ Y
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{:
}\
\textbf{\textcolor{Black
}{x
}}\textcolor{BrickRed
}{(
}X
\textcolor{BrickRed
}{),
}\
\textbf{\textcolor{Black
}{y
}}\textcolor{BrickRed
}{(
}Y
\textcolor{BrickRed
}{)
}\
\textcolor{Red
}{\
{\
}} \\
23 \mbox{}\textcolor{Red
}{\
}}\textcolor{BrickRed
}{;
} \\
25 \mbox{}point\ pivot
\textcolor{BrickRed
}{;
} \\
27 \mbox{}ostream
\textcolor{BrickRed
}{\&
}\
\textbf{\textcolor{Blue
}{operator
}}\textcolor{BrickRed
}{$<$$<$
}\
\textcolor{BrickRed
}{(
}ostream
\textcolor{BrickRed
}{\&
}\ out
\textcolor{BrickRed
}{,
}\
\textbf{\textcolor{Blue
}{const
}}\ point
\textcolor{BrickRed
}{\&
}\ c
\textcolor{BrickRed
}{)
} \\
28 \mbox{}\textcolor{Red
}{\
{} \\
29 \mbox{}\ \ out\
\textcolor{BrickRed
}{$<$$<$
}\
\texttt{\textcolor{Red
}{"
{}("
{}}}\
\textcolor{BrickRed
}{$<$$<$
}\ c
\textcolor{BrickRed
}{.
}x\
\textcolor{BrickRed
}{$<$$<$
}\
\texttt{\textcolor{Red
}{"
{},"
{}}}\
\textcolor{BrickRed
}{$<$$<$
}\ c
\textcolor{BrickRed
}{.
}y\
\textcolor{BrickRed
}{$<$$<$
}\
\texttt{\textcolor{Red
}{"
{})"
{}}}\textcolor{BrickRed
}{;
} \\
30 \mbox{}\ \
\textbf{\textcolor{Blue
}{return
}}\ out
\textcolor{BrickRed
}{;
} \\
31 \mbox{}\textcolor{Red
}{\
}} \\
33 \mbox{}\textbf{\textcolor{Blue
}{inline
}}\
\textcolor{ForestGreen
}{int
}\
\textbf{\textcolor{Black
}{distsqr
}}\textcolor{BrickRed
}{(
}\textbf{\textcolor{Blue
}{const
}}\ point\
\textcolor{BrickRed
}{\&
}a
\textcolor{BrickRed
}{,
}\
\textbf{\textcolor{Blue
}{const
}}\ point\
\textcolor{BrickRed
}{\&
}b
\textcolor{BrickRed
}{)
}\textcolor{Red
}{\
{} \\
34 \mbox{}\ \
\textbf{\textcolor{Blue
}{return
}}\
\textcolor{BrickRed
}{(
}a
\textcolor{BrickRed
}{.
}x\
\textcolor{BrickRed
}{-
}\ b
\textcolor{BrickRed
}{.
}x
\textcolor{BrickRed
}{)*(
}a
\textcolor{BrickRed
}{.
}x\
\textcolor{BrickRed
}{-
}\ b
\textcolor{BrickRed
}{.
}x
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{+
}\
\textcolor{BrickRed
}{(
}a
\textcolor{BrickRed
}{.
}y\
\textcolor{BrickRed
}{-
}\ b
\textcolor{BrickRed
}{.
}y
\textcolor{BrickRed
}{)*(
}a
\textcolor{BrickRed
}{.
}y\
\textcolor{BrickRed
}{-
}\ b
\textcolor{BrickRed
}{.
}y
\textcolor{BrickRed
}{);
} \\
35 \mbox{}\textcolor{Red
}{\
}} \\
37 \mbox{}\textbf{\textcolor{Blue
}{inline
}}\
\textcolor{ForestGreen
}{double
}\
\textbf{\textcolor{Black
}{dist
}}\textcolor{BrickRed
}{(
}\textbf{\textcolor{Blue
}{const
}}\ point\
\textcolor{BrickRed
}{\&
}a
\textcolor{BrickRed
}{,
}\
\textbf{\textcolor{Blue
}{const
}}\ point\
\textcolor{BrickRed
}{\&
}b
\textcolor{BrickRed
}{)
}\textcolor{Red
}{\
{} \\
38 \mbox{}\ \
\textbf{\textcolor{Blue
}{return
}}\
\textbf{\textcolor{Black
}{sqrt
}}\textcolor{BrickRed
}{(
}\textbf{\textcolor{Black
}{distsqr
}}\textcolor{BrickRed
}{(
}a
\textcolor{BrickRed
}{,
}\ b
\textcolor{BrickRed
}{));
} \\
39 \mbox{}\textcolor{Red
}{\
}} \\
41 \mbox{}\textit{\textcolor{Brown
}{//retorna\ $>$\
0\ si\ c\ esta\ a\ la\ izquierda\ del\ segmento\ AB
}} \\
42 \mbox{}\textit{\textcolor{Brown
}{//retorna\ $<$\
0\ si\ c\ esta\ a\ la\ derecha\ del\ segmento\ AB
}} \\
43 \mbox{}\textit{\textcolor{Brown
}{//retorna\ ==\
0\ si\ c\ es\ colineal\ con\ el\ segmento\ AB
}} \\
44 \mbox{}\textbf{\textcolor{Blue
}{inline
}}\
\textcolor{ForestGreen
}{int
}\
\textbf{\textcolor{Black
}{cross
}}\textcolor{BrickRed
}{(
}\textbf{\textcolor{Blue
}{const
}}\ point\
\textcolor{BrickRed
}{\&
}a
\textcolor{BrickRed
}{,
}\
\textbf{\textcolor{Blue
}{const
}}\ point\
\textcolor{BrickRed
}{\&
}b
\textcolor{BrickRed
}{,
}\
\textbf{\textcolor{Blue
}{const
}}\ point\
\textcolor{BrickRed
}{\&
}c
\textcolor{BrickRed
}{)
}\textcolor{Red
}{\
{} \\
45 \mbox{}\ \
\textbf{\textcolor{Blue
}{return
}}\
\textcolor{BrickRed
}{(
}b
\textcolor{BrickRed
}{.
}x
\textcolor{BrickRed
}{-
}a
\textcolor{BrickRed
}{.
}x
\textcolor{BrickRed
}{)*(
}c
\textcolor{BrickRed
}{.
}y
\textcolor{BrickRed
}{-
}a
\textcolor{BrickRed
}{.
}y
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{-
}\
\textcolor{BrickRed
}{(
}c
\textcolor{BrickRed
}{.
}x
\textcolor{BrickRed
}{-
}a
\textcolor{BrickRed
}{.
}x
\textcolor{BrickRed
}{)*(
}b
\textcolor{BrickRed
}{.
}y
\textcolor{BrickRed
}{-
}a
\textcolor{BrickRed
}{.
}y
\textcolor{BrickRed
}{);
} \\
46 \mbox{}\textcolor{Red
}{\
}} \\
48 \mbox{}\textit{\textcolor{Brown
}{//Self\ $<$\ that\ si\ esta\ a\ la\ derecha\ del\ segmento\ Pivot-That
}} \\
49 \mbox{}\textcolor{ForestGreen
}{bool
}\
\textbf{\textcolor{Black
}{angleCmp
}}\textcolor{BrickRed
}{(
}\textbf{\textcolor{Blue
}{const
}}\ point\
\textcolor{BrickRed
}{\&
}self
\textcolor{BrickRed
}{,
}\
\textbf{\textcolor{Blue
}{const
}}\ point\
\textcolor{BrickRed
}{\&
}that
\textcolor{BrickRed
}{)
}\textcolor{Red
}{\
{} \\
50 \mbox{}\ \
\textcolor{ForestGreen
}{int
}\ t\
\textcolor{BrickRed
}{=
}\
\textbf{\textcolor{Black
}{cross
}}\textcolor{BrickRed
}{(
}pivot
\textcolor{BrickRed
}{,
}\ that
\textcolor{BrickRed
}{,
}\ self
\textcolor{BrickRed
}{);
} \\
51 \mbox{}\ \
\textbf{\textcolor{Blue
}{if
}}\
\textcolor{BrickRed
}{(
}t\
\textcolor{BrickRed
}{$<$
}\
\textcolor{Purple
}{0}\textcolor{BrickRed
}{)
}\
\textbf{\textcolor{Blue
}{return
}}\
\textbf{\textcolor{Blue
}{true
}}\textcolor{BrickRed
}{;
} \\
52 \mbox{}\ \
\textbf{\textcolor{Blue
}{if
}}\
\textcolor{BrickRed
}{(
}t\
\textcolor{BrickRed
}{==
}\
\textcolor{Purple
}{0}\textcolor{BrickRed
}{)
}\textcolor{Red
}{\
{} \\
53 \mbox{}\ \ \ \
\textit{\textcolor{Brown
}{//Self\ $<$\ that\ si\ está\ más\ cerquita
}} \\
54 \mbox{}\ \ \ \
\textbf{\textcolor{Blue
}{return
}}\
\textcolor{BrickRed
}{(
}\textbf{\textcolor{Black
}{distsqr
}}\textcolor{BrickRed
}{(
}pivot
\textcolor{BrickRed
}{,
}\ self
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{$<$
}\
\textbf{\textcolor{Black
}{distsqr
}}\textcolor{BrickRed
}{(
}pivot
\textcolor{BrickRed
}{,
}\ that
\textcolor{BrickRed
}{));
} \\
55 \mbox{}\ \
\textcolor{Red
}{\
}} \\
56 \mbox{}\ \
\textbf{\textcolor{Blue
}{return
}}\
\textbf{\textcolor{Blue
}{false
}}\textcolor{BrickRed
}{;
} \\
57 \mbox{}\textcolor{Red
}{\
}} \\
59 \mbox{}vector
\textcolor{BrickRed
}{$<$
}point
\textcolor{BrickRed
}{$>$
}\
\textbf{\textcolor{Black
}{graham
}}\textcolor{BrickRed
}{(
}vector
\textcolor{BrickRed
}{$<$
}point
\textcolor{BrickRed
}{$>$
}\ p
\textcolor{BrickRed
}{)
}\textcolor{Red
}{\
{} \\
60 \mbox{}\ \
\textit{\textcolor{Brown
}{//Metemos\ el\ más\ abajo\ más\ a\ la\ izquierda\ en\ la\ posición\
0}} \\
61 \mbox{}\ \
\textbf{\textcolor{Blue
}{for
}}\
\textcolor{BrickRed
}{(
}\textcolor{ForestGreen
}{int
}\ i
\textcolor{BrickRed
}{=
}\textcolor{Purple
}{1}\textcolor{BrickRed
}{;
}\ i
\textcolor{BrickRed
}{$<$
}p
\textcolor{BrickRed
}{.
}\textbf{\textcolor{Black
}{size
}}\textcolor{BrickRed
}{();
}\
\textcolor{BrickRed
}{++
}i
\textcolor{BrickRed
}{)
}\textcolor{Red
}{\
{} \\
62 \mbox{}\ \ \ \
\textbf{\textcolor{Blue
}{if
}}\
\textcolor{BrickRed
}{(
}p
\textcolor{BrickRed
}{[}i
\textcolor{BrickRed
}{].
}y\
\textcolor{BrickRed
}{$<$
}\ p
\textcolor{BrickRed
}{[}\textcolor{Purple
}{0}\textcolor{BrickRed
}{].
}y\
\textcolor{BrickRed
}{$|$$|$
}\
\textcolor{BrickRed
}{(
}p
\textcolor{BrickRed
}{[}i
\textcolor{BrickRed
}{].
}y\
\textcolor{BrickRed
}{==
}\ p
\textcolor{BrickRed
}{[}\textcolor{Purple
}{0}\textcolor{BrickRed
}{].
}y\
\textcolor{BrickRed
}{\&\&
}\ p
\textcolor{BrickRed
}{[}i
\textcolor{BrickRed
}{].
}x\
\textcolor{BrickRed
}{$<$
}\ p
\textcolor{BrickRed
}{[}\textcolor{Purple
}{0}\textcolor{BrickRed
}{].
}x\
\textcolor{BrickRed
}{))
} \\
63 \mbox{}\ \ \ \ \ \
\textbf{\textcolor{Black
}{swap
}}\textcolor{BrickRed
}{(
}p
\textcolor{BrickRed
}{[}\textcolor{Purple
}{0}\textcolor{BrickRed
}{],
}\ p
\textcolor{BrickRed
}{[}i
\textcolor{BrickRed
}{]);
} \\
64 \mbox{}\ \
\textcolor{Red
}{\
}} \\
66 \mbox{}\ \ pivot\
\textcolor{BrickRed
}{=
}\ p
\textcolor{BrickRed
}{[}\textcolor{Purple
}{0}\textcolor{BrickRed
}{];
} \\
67 \mbox{}\ \
\textbf{\textcolor{Black
}{sort
}}\textcolor{BrickRed
}{(
}p
\textcolor{BrickRed
}{.
}\textbf{\textcolor{Black
}{begin
}}\textcolor{BrickRed
}{(),
}\ p
\textcolor{BrickRed
}{.
}\textbf{\textcolor{Black
}{end
}}\textcolor{BrickRed
}{(),
}\ angleCmp
\textcolor{BrickRed
}{);
} \\
69 \mbox{}\ \
\textit{\textcolor{Brown
}{//Ordenar\ por\ ángulo\ y\ eliminar\ repetidos.
}} \\
70 \mbox{}\ \
\textit{\textcolor{Brown
}{//Si\ varios\ puntos\ tienen\ el\ mismo\ angulo\ el\ más\ lejano\ queda\ después\ en\ la\ lista
}} \\
71 \mbox{}\ \ vector
\textcolor{BrickRed
}{$<$
}point
\textcolor{BrickRed
}{$>$
}\
\textbf{\textcolor{Black
}{chull
}}\textcolor{BrickRed
}{(
}p
\textcolor{BrickRed
}{.
}\textbf{\textcolor{Black
}{begin
}}\textcolor{BrickRed
}{(),
}\ p
\textcolor{BrickRed
}{.
}\textbf{\textcolor{Black
}{begin
}}\textcolor{BrickRed
}{()+
}\textcolor{Purple
}{3}\textcolor{BrickRed
}{);
} \\
73 \mbox{}\ \
\textit{\textcolor{Brown
}{//Ahora\ sí!!!
}} \\
74 \mbox{}\ \
\textbf{\textcolor{Blue
}{for
}}\
\textcolor{BrickRed
}{(
}\textcolor{ForestGreen
}{int
}\ i
\textcolor{BrickRed
}{=
}\textcolor{Purple
}{3}\textcolor{BrickRed
}{;
}\ i
\textcolor{BrickRed
}{$<$
}p
\textcolor{BrickRed
}{.
}\textbf{\textcolor{Black
}{size
}}\textcolor{BrickRed
}{();
}\
\textcolor{BrickRed
}{++
}i
\textcolor{BrickRed
}{)
}\textcolor{Red
}{\
{} \\
75 \mbox{}\ \ \ \
\textbf{\textcolor{Blue
}{while
}}\
\textcolor{BrickRed
}{(
}\ chull
\textcolor{BrickRed
}{.
}\textbf{\textcolor{Black
}{size
}}\textcolor{BrickRed
}{()
}\
\textcolor{BrickRed
}{$>$=
}\
\textcolor{Purple
}{2}\
\textcolor{BrickRed
}{\&\&
}\
\textbf{\textcolor{Black
}{cross
}}\textcolor{BrickRed
}{(
}chull
\textcolor{BrickRed
}{[}chull
\textcolor{BrickRed
}{.
}\textbf{\textcolor{Black
}{size
}}\textcolor{BrickRed
}{()-
}\textcolor{Purple
}{2}\textcolor{BrickRed
}{],
}\ chull
\textcolor{BrickRed
}{[}chull
\textcolor{BrickRed
}{.
}\textbf{\textcolor{Black
}{size
}}\textcolor{BrickRed
}{()-
}\textcolor{Purple
}{1}\textcolor{BrickRed
}{],
}\ p
\textcolor{BrickRed
}{[}i
\textcolor{BrickRed
}{])
}\
\textcolor{BrickRed
}{$<$=
}\
\textcolor{Purple
}{0}\textcolor{BrickRed
}{)
}\textcolor{Red
}{\
{} \\
76 \mbox{}\ \ \ \ \ \ chull
\textcolor{BrickRed
}{.
}\textbf{\textcolor{Black
}{erase
}}\textcolor{BrickRed
}{(
}chull
\textcolor{BrickRed
}{.
}\textbf{\textcolor{Black
}{end
}}\textcolor{BrickRed
}{()
}\
\textcolor{BrickRed
}{-
}\
\textcolor{Purple
}{1}\textcolor{BrickRed
}{);
} \\
77 \mbox{}\ \ \ \
\textcolor{Red
}{\
}} \\
78 \mbox{}\ \ \ \ chull
\textcolor{BrickRed
}{.
}\textbf{\textcolor{Black
}{push$
\_$back
}}\textcolor{BrickRed
}{(
}p
\textcolor{BrickRed
}{[}i
\textcolor{BrickRed
}{]);
} \\
79 \mbox{}\ \
\textcolor{Red
}{\
}} \\
80 \mbox{}\ \
\textit{\textcolor{Brown
}{//chull\ contiene\ los\ puntos\ del\ convex\ hull\ ordenados\ anti-clockwise.
}} \\
81 \mbox{}\ \
\textit{\textcolor{Brown
}{//No\ contiene\ ningún\ punto\ repetido.
}} \\
82 \mbox{}\ \
\textit{\textcolor{Brown
}{//El\ primer\ punto\ no\ es\ el\ mismo\ que\ el\ último,\ i.e,\ la\ última\ arista
}} \\
83 \mbox{}\ \
\textit{\textcolor{Brown
}{//va\ de\ chull
[chull.size()-
1]\ a\ chull
[0]}} \\
84 \mbox{}\ \
\textbf{\textcolor{Blue
}{return
}}\ chull
\textcolor{BrickRed
}{;
} \\
85 \mbox{}\textcolor{Red
}{\
}} \\
87 } \normalfont\normalsize